//
//  RadioViewController.m
//  OurRelax
//
//  Created by 于宾尧 on 15/6/16.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()
@property (nonatomic, retain) UITableView *radioTV;
@end

@implementation RadioViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self subMyViews];
        self.headerViewArray = [NSMutableArray array];
        self.radioTypeArray = [NSMutableArray array];
    }
    return self;
}

-(void)subMyViews
{
    [self getHeaderViewImage];
    [self getRadioInfo];
    self.headerView = [[HeaderScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    [self.view addSubview:self.headerView];
    [self.headerView release];

    
     self.radioTV = [[UITableView alloc]initWithFrame:CGRectMake(5, 10, self.view.frame.size.width-10, self.view.frame.size.height) style:UITableViewStyleGrouped ];

    _radioTV.delegate = self;
    _radioTV.dataSource = self;
    _radioTV.tableHeaderView = self.headerView;
    [self.view addSubview:_radioTV];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    RadioModels *radioMdl = [self.radioTypeArray objectAtIndex:section];
    NSString *str = radioMdl.tag_type_name;
    
    return str;
}

-(void)getRadioInfo
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSURL *url = [NSURL URLWithString:@"http://fm.api.ttpod.com/radiolist?image_type=240_200&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=c5bdf2871397a7699d43e8f620fa201f563e3bd2&idfa=76882D7D-3E10-45D4-B40B-20A0394D00CF&utdid=VXlTA7t9%2BlEDAE055roWXtEe&alf=201200&bundle_id=com.ttpod.music"];
    NSString *urlString = [NSString stringWithFormat:@"%@",url];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"data"];
        
        
        for (NSMutableDictionary *smallDic in array) {
            RadioModels *radioMdl = [[RadioModels alloc]init];
            [radioMdl setValuesForKeysWithDictionary:smallDic];
            [self.radioTypeArray addObject:radioMdl];
        
        }
        
        
        [self.radioTV reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
}

-(void)getHeaderViewImage
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSURL *url = [NSURL URLWithString:@"http://online.dongting.com/recomm/recomm_modules?app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=c5bdf2871397a7699d43e8f620fa201f563e3bd2&idfa=76882D7D-3E10-45D4-B40B-20A0394D00CF&utdid=VXlTA7t9%2BlEDAE055roWXtEe&alf=201200&bundle_id=com.ttpod.music&version=0"];
    NSString *urlString = [NSString stringWithFormat:@"%@",url];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *smallDic = [bigDic objectForKey:@"data"];
        NSMutableArray *songDic = [smallDic objectForKey:@"songlists"];
        NSMutableDictionary *ViewDic = [songDic objectAtIndex:0];
        NSMutableArray *dataArr = [ViewDic objectForKey:@"data"];
        for (NSMutableDictionary *dataDic in dataArr) {
            HeaderView *headerModel = [[HeaderView alloc]init];
            [headerModel setValuesForKeysWithDictionary:dataDic];
            [self.headerViewArray addObject:headerModel];
        }
        [self.headerView setImages:self.headerViewArray];

        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 90;
    }
    return 150;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.radioTypeArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
    
    static NSString *cellIndefiner = @"radioCell";
    RadioTableViewCell *radioCell = [tableView dequeueReusableCellWithIdentifier:cellIndefiner];
    if (radioCell == nil) {
        radioCell = [[RadioTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndefiner];
        
        
    }
        return radioCell;
    }
    static NSString *cellIndefiner = @"radioCell2";
    BigRadioCell *radioCell = [tableView dequeueReusableCellWithIdentifier:cellIndefiner];
    if (radioCell == nil) {
        radioCell = [[BigRadioCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndefiner];
        
    }
    return radioCell;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
